IniLib Module

History

current version 1.6 - 31st January 2020

version date comment
1.0 01/Feb/2007 Original code
1.1 09/Mar/2007 support for sections and subsections
1.2 25/Oct/2008 support for multiple ini files using type IniList
1.3 11/Aug/2009 Added SectionIsPresent
1.4 19/Oct/2017 Added SubSectionIsPresent
1.5 24/Nov/2017 Added IniOpenFileUnit
1.6 31/Jan/2020 Functions GetNofSections, GetNofSubSections

License

license: GNU GPL http://www.gnu.org/licenses/

This file is part of

MOSAICO -- MOdular library for raSter bAsed hydrologIcal appliCatiOn.

Copyright (C) 2011 Giovanni Ravazzani

Module Description

routines for reading configuration files with each line of the form line 'name = value' with support for sections [] and subsections [[ ]]. Comments are denoted by '#' and can occupy a an entire line or terminating one example file:

 # ini file example 
  key1 = value1
  key2 = value2
    [section1]   # first section
      key1 = value3
      key2 = value4
    [section2]   # second section
      key1 = value5
      key2 = value6
      [[subsection]] # subsection in section1
        key1 = value7
        key2 = value8

Adapted from Antony Lewis (http://cosmologist.info/)



Variables

Type Visibility Attributes Name Initial
logical, private :: inSection
logical, private :: inSubSection

define a dynamic list to store elements in memory

integer(kind=long), private :: ios
integer(kind=long), private :: numKeys = 0
integer(kind=long), private, parameter :: stringLen = 500

Interfaces

public interface IniOpen

  • private subroutine IniOpenFileName(fileName, iniDB)

    open and read a ini file

    Arguments

    Type IntentOptional Attributes Name
    character(len=*), intent(in) :: fileName
    type(IniList), intent(out) :: iniDB
  • private subroutine IniOpenFileUnit(fileUnit, iniDB)

    read a ini file already open

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=short), intent(in) :: fileUnit
    type(IniList), intent(out) :: iniDB

Derived Types

type, public ::  IniList

Components

Type Visibility Attributes Name Initial
character(len=stringLen), public, POINTER :: keys(:)
integer(kind=long), public :: nOfSections
integer(kind=long), public :: nOfSubSections
integer(kind=long), public :: numKeys
integer(kind=long), public, POINTER :: sectionBegin(:)
integer(kind=long), public, POINTER :: sectionEnd(:)
character(len=stringLen), public, POINTER :: sectionName(:)
integer(kind=long), public, POINTER :: subSectionBegin(:)
integer(kind=long), public, POINTER :: subSectionEnd(:)
character(len=stringLen), public, POINTER :: subSectionName(:)
character(len=stringLen), public, POINTER :: vals(:)

Functions

public function GetNofSections(ini) result(count)

count sections in a ini db

Arguments

Type IntentOptional Attributes Name
type(IniList), intent(in) :: ini

Return Value integer(kind=short)

public function GetNofSubSections(ini, sectionname) result(count)

return number of subsections within a section of a ini db

Arguments

Type IntentOptional Attributes Name
type(IniList), intent(in) :: ini
character(len=*), intent(in) :: sectionname

Return Value integer(kind=short)

public function IniReadDouble(key, iniDB, section, subSection, default)

read an double precision number corresponding to Key

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection
real(kind=double), intent(in), optional :: default

Return Value real(kind=double)

public function IniReadInt(key, iniDB, section, subSection, default)

read an integer corresponding to Key

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection
integer(kind=long), intent(in), optional :: default

Return Value integer(kind=long)

public function IniReadLogical(key, iniDB, section, subSection, default)

read a logical value corresponding to Key

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection
logical, intent(in), optional :: default

Return Value logical

public function IniReadString(key, iniDB, section, subSection)

read a string corresponding to Key

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection

Return Value character(len=stringLen)

public function KeyIsPresent(key, iniDB, section, subSection) result(isHere)

return true if key is present, false otherwise

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection

Return Value logical

public function SectionIsPresent(section, iniDB) result(isHere)

return true if section is present, false otherwise

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: section
type(IniList), intent(in) :: iniDB

Return Value logical

public function SubSectionIsPresent(subsection, section, iniDB) result(isHere)

return true if subsection is present in section, false otherwise

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: subsection
character(len=*), intent(in) :: section
type(IniList), intent(in) :: iniDB

Return Value logical

public function iniReadReal(key, iniDB, section, subSection, default) result(IniReadReal)

read a real number corresponding to Key

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: key
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: section
character(len=*), intent(in), optional :: subSection
real(kind=float), intent(in), optional :: default

Return Value real(kind=float)

private function IniCountKeys(unit) result(count)

count Key-Val pair in a file

Arguments

Type IntentOptional Attributes Name
integer(kind=short), intent(in) :: unit

Return Value integer(kind=long)


Subroutines

public subroutine IniClose(iniDB)

close a ini file

Arguments

Type IntentOptional Attributes Name
type(IniList), intent(out) :: iniDB

public subroutine Sync(first, last, iniDB, sec, subSec)

synchronize the window in which searching for the key

Arguments

Type IntentOptional Attributes Name
integer(kind=long), intent(out) :: first
integer(kind=long), intent(out) :: last
type(IniList), intent(in) :: iniDB
character(len=*), intent(in), optional :: sec
character(len=*), intent(in), optional :: subSec

private subroutine CheckClosure(iniDB)

check if section and subsection still opened

Arguments

Type IntentOptional Attributes Name
type(IniList), intent(inout) :: iniDB

private subroutine IniAddLine(aInLine, iniDB)

add a new Key-Val pair

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: aInLine
type(IniList), intent(out) :: iniDB

private subroutine IniOpenFileName(fileName, iniDB)

open and read a ini file

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: fileName
type(IniList), intent(out) :: iniDB

private subroutine IniOpenFileUnit(fileUnit, iniDB)

read a ini file already open

Arguments

Type IntentOptional Attributes Name
integer(kind=short), intent(in) :: fileUnit
type(IniList), intent(out) :: iniDB